package com.xinqi.modules.sys.dict.controller.admin;

import com.xinqi.common.base.constants.ServiceNameConstants;
import com.xinqi.common.base.data.IdNameDTO;
import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.R;
import com.xinqi.common.log.syslog.annotation.Log;
import com.xinqi.common.log.syslog.enums.BusinessType;
import com.xinqi.common.log.syslog.enums.OperatorType;
import com.xinqi.modules.sys.dict.dto.req.AddStageDTO;
import com.xinqi.modules.sys.dict.dto.req.DictVersionCreateDTO;
import com.xinqi.modules.sys.dict.dto.req.DictVersionQueryDTO;
import com.xinqi.modules.sys.dict.dto.req.DictVersionUpdateDTO;
import com.xinqi.modules.sys.dict.dto.rsp.DictGradeResultDTO;
import com.xinqi.modules.sys.dict.dto.rsp.DictStageResultDTO;
import com.xinqi.modules.sys.dict.dto.rsp.DictSubjectResultDTO;
import com.xinqi.modules.sys.dict.dto.rsp.DictVersionResultDTO;
import com.xinqi.modules.sys.dict.service.DictGradeService;
import com.xinqi.modules.sys.dict.service.DictStageService;
import com.xinqi.modules.sys.dict.service.DictVersionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 字典-版本Admin控制层
 *
 * @author: linkai
 * @date: 2023/05/07
 */
@Tag(name = "Admin-系统字典", description = "系统字典Admin接口")
@RestController
@RequestMapping("/admin/dict-version")
@RequiredArgsConstructor
public class DictVersionAdminController {

    private final DictVersionService dictVersionService;

    private final DictStageService dictStageService;

    private final DictGradeService dictGradeService;

    /**
     * 字典-版本-分页
     */
    @Operation(summary = "字典-版本-分页")
    @GetMapping("/page")
    public R<PageInfo<DictVersionResultDTO>> page(@ParameterObject DictVersionQueryDTO dto) {
        PageInfo<DictVersionResultDTO> page = dictVersionService.page(dto, dto);
        return R.data(page);
    }

    /**
     * 字典-版本-详情
     */
    @Operation(summary = "字典-版本-详情")
    @GetMapping("/{id}")
    public R<DictVersionResultDTO> findById(@Parameter(description = "id", example = "100001") @PathVariable("id") Long id) {
        DictVersionResultDTO dto = dictVersionService.findById(id);
        return R.data(dto);
    }

    /**
     * 新增字典-版本
     */
    @Operation(summary = "新增字典-版本")
    @PostMapping
    @Log(title = ServiceNameConstants.XQ_SYS + " -- 新增字典-版本", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    public R<Long> insert(@RequestBody DictVersionCreateDTO dto) {
        return R.data(dictVersionService.insert(dto));
    }

    /**
     * 修改字典-版本
     */
    @Operation(summary = "修改字典-版本")
    @PutMapping("/{id}")
    @Log(title = ServiceNameConstants.XQ_SYS + " -- 修改字典-版本", businessType = BusinessType.UPDATE, operatorType = OperatorType.MANAGE)
    public R<Boolean> update(@Parameter(description = "id", example = "100001") @PathVariable("id") Long id,
                             @RequestBody DictVersionUpdateDTO dto) {
        dto.setId(id);
        return R.data(dictVersionService.updateById(dto));
    }

    /**
     * 删除字典-版本
     */
    @Operation(summary = "删除字典-版本")
    @DeleteMapping("/{id}")
    @Log(title = ServiceNameConstants.XQ_SYS + " -- 删除字典-版本", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    public R<Boolean> delete(@Parameter(description = "id", example = "100001") @PathVariable("id") Long id) {
        return R.data(dictVersionService.deleteById(id));
    }

    @Operation(summary = "查询版本中的学段")
    @GetMapping("/{id}/stage")
    public R<List<DictStageResultDTO>> findStageByVersionId(@PathVariable("id") Long id) {
        List<DictStageResultDTO> list = dictVersionService.findStageByVersionId(id);
        return R.data(list);
    }

    @Operation(summary = "根据版本ID获取云课本已绑定学段")
    @GetMapping("/{id}/bind-stage")
    public R<List<DictStageResultDTO>> findBindStageByVersionId(@PathVariable("id") Long id) {
        List<DictStageResultDTO> list = dictVersionService.findBindStageByVersionId(id);
        return R.data(list);
    }

    @Operation(summary = "查询版本中的年级")
    @GetMapping("/{id}/grade")
    public R<List<DictGradeResultDTO>> findGradeByVersionId(@PathVariable("id") Long id) {
        return R.data(dictStageService.findGradeByVersionId(id));
    }

    @Operation(summary = "查询版本中的学科")
    @GetMapping("/{id}/Subject")
    public R<List<DictSubjectResultDTO>> findSubjectByVersionId(@PathVariable("id") Long id) {
        return R.data(dictGradeService.findSubjectByVersionId(id));
    }

    /**
     * 添加版本中的学段
     */
    @Operation(summary = "添加版本中的学段")
    @PostMapping("/{id}/stage")
    @Log(title = ServiceNameConstants.XQ_SYS + " -- 添加版本中的学段", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    public R<Boolean> addStage(@Parameter(description = "id", example = "100001") @PathVariable("id") Long id,
                               @RequestBody AddStageDTO dto) {
        dto.setVersionId(id);
        return R.data(dictVersionService.addStage(dto));
    }

    /**
     * 字典-版本-名称是否存在
     */
    @Operation(summary = "字典-版本-名称是否存在")
    @GetMapping("/is-exists-name")
    public R<Boolean> isExistName(@ParameterObject IdNameDTO dto) {
        return R.data(dictVersionService.isExistName(dto.getName(), dto.getId()));
    }

}
